package com.umeng.comm.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qianxx.draw.R;
import com.umeng.comm.core.beans.CommConfig;
import com.umeng.comm.core.beans.MessageCount;
import com.umeng.comm.core.constants.Constants;
import com.umeng.comm.ui.activities.NewMsgActivity;
import com.umeng.comm.ui.activities.NewMsgActivity_old;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by summersoft on 2016/4/15.
 */
public class NewMsgFragments extends Fragment {

    public MessageCount mUnreadMsg = CommConfig.getConfig().mMessageCount;

    private View mView;
    private TextView mTitleView;
    private RelativeLayout commentLay;
    private RelativeLayout loveLay;
    private RelativeLayout atLay;
    private RelativeLayout noticeLay;
    public List<TextView> mDots = new ArrayList<TextView>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.newmsg_fragment, container, false);
        initView();
        return mView;

    }

    private void initView() {
        findDots(); // 查找Dots控件
        mTitleView = (TextView) mView.findViewById(R.id.umeng_comm_title_tv);
        mTitleView.setText("消息");
        commentLay = (RelativeLayout) mView.findViewById(R.id.umeng_comm_my_msg_comment);
//        updateUnreadMsgCount(0);
//        updateDotsVisiable(0, ResFinder.getId("umeng_comm_my_msg_badge_view_comment"));
        commentLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity().getApplicationContext(), NewMsgActivity.class);
                intent.putExtra("type", 0);
                startActivity(intent);
                updateUnreadMsgCount(0);
            }
        });
        loveLay = (RelativeLayout) mView.findViewById(R.id.umeng_comm_my_msg_like);
//        updateUnreadMsgCount(1);
//        updateDotsVisiable(1, ResFinder.getId("umeng_comm_my_msg_badge_view_like"));
        loveLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity().getApplicationContext(), NewMsgActivity.class);
                intent.putExtra("type", 1);
                getActivity().startActivity(intent);
                updateUnreadMsgCount(1);
            }
        });
        atLay = (RelativeLayout) mView.findViewById(R.id.umeng_comm_my_msg_at);
//        updateUnreadMsgCount(2);
//        updateDotsVisiable(2, ResFinder.getId("umeng_comm_my_msg_badge_view_at"));
        TextView mAt = (TextView) mView.findViewById(R.id.umeng_comm_my_msg_text_view_at);
        mAt.setText("@我");
        atLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity().getApplicationContext(), NewMsgActivity.class);
                intent.putExtra("type", 2);
                getActivity().startActivity(intent);
                updateUnreadMsgCount(2);
            }
        });
        noticeLay = (RelativeLayout) mView.findViewById(R.id.umeng_comm_my_msg_notice);
//        updateUnreadMsgCount(3);
//        updateDotsVisiable(3, ResFinder.getId("umeng_comm_my_msg_badge_view_notice"));
        noticeLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity().getApplicationContext(), NewMsgActivity.class);
                intent.putExtra("type", 3);
                startActivity(intent);
                updateUnreadMsgCount(3);
            }
        });

        mView.findViewById(R.id.set_title_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity().getApplicationContext(), NewMsgActivity_old.class);
                intent.putExtra(Constants.USER, CommConfig.getConfig().loginedUser);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        setAllDots(); // 更新所有dots的显示
    }

    private void findDots() {
        int[] resId = new int[]{R.id.umeng_comm_my_msg_badge_view_comment
                , R.id.umeng_comm_my_msg_badge_view_like
                , R.id.umeng_comm_my_msg_badge_view_at
                , R.id.umeng_comm_my_msg_badge_view_notice};
        mDots.clear();
        for (int i = 0; i < resId.length; i++) {
            TextView view = (TextView) mView.findViewById(resId[i]);
            mDots.add(view); // 添加到mDots中
        }
    }

    private void setAllDots() {
        for (int i = 0; i < mDots.size(); i++) {
            setDotsVisiable(i);
        }
    }

    public void setDotsVisiable(int position) {
        int unReadMsgCount = 0;
        if (mUnreadMsg != null) {
            switch (position) {
                case 0:
                    unReadMsgCount = mUnreadMsg.unReadCommentsCount;
                    break;
                case 1:
                    unReadMsgCount = mUnreadMsg.unReadLikesCount;
                    break;
                case 2:
                    unReadMsgCount = mUnreadMsg.unReadAtCount;
                    break;
                case 3:
                    unReadMsgCount = mUnreadMsg.unReadNotice;
                    break;
                default:
                    break;
            }
        }
        TextView dot = mDots.get(position);
        if (dot != null) {
            if (unReadMsgCount > 0) {
                dot.setVisibility(View.VISIBLE);
                String str = unReadMsgCount >= 100 ? "..." : String.valueOf(unReadMsgCount);
                dot.setText(str);
            } else {
                dot.setVisibility(View.GONE);
            }
        }
    }

    public void updateUnreadMsgCount(int index) {
        switch (index) {
            case 0:
                mUnreadMsg.unReadCommentsCount = 0;
                break;

            case 1:
                mUnreadMsg.unReadLikesCount = 0;
                break;

            case 2:
                mUnreadMsg.unReadAtCount = 0;
                break;

            case 3:
                mUnreadMsg.unReadNotice = 0;
                break;

//            case 3:
//
//                break;


            default:
                break;
        }
        mUnreadMsg.unReadSessionsCount = 0;
        mUnreadMsg.unReadTotal = mUnreadMsg.unReadAtCount + mUnreadMsg.unReadCommentsCount
                + mUnreadMsg.unReadLikesCount + mUnreadMsg.unReadNotice +
                mUnreadMsg.newFansCount + mUnreadMsg.unReadSessionsCount;

        setDotsVisiable(index); // 更新显示
    }

}
